package com.pzl.projection.player;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.nio.ByteBuffer;

//网上的免费websocket 服务地址  ws://121.40.165.18:8800

public class MainActivity extends AppCompatActivity implements NetReceiver.INet {
    final String TAG = "jinx_projection";
    Surface surface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SurfaceView surfaceView = findViewById(R.id.surfaceViewProjectionPlayer);
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceCreated ");
                surface = holder.getSurface();
                initDecoder(surface, 720, 1280);
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
            }
        });

    }

    public void click(View view) {
        EditText et = findViewById(R.id.et_projection_player_ip);
        String ip = et.getText().toString().trim();
        Log.i(TAG, "ip = " + ip);
        NetReceiver netReceiver = new NetReceiver(MainActivity.this, ip);
        netReceiver.start();
    }


    MediaCodec mediaCodec;

    public void initDecoder(Surface surface, int width, int height) {
        try {
            mediaCodec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_HEVC);
            final MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_HEVC, width, height);
            format.setInteger(MediaFormat.KEY_BIT_RATE, width * height);
            format.setInteger(MediaFormat.KEY_FRAME_RATE, 20);
            format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
            mediaCodec.configure(format,
                    surface,
                    null, 0);
            mediaCodec.start();
        } catch (
                IOException e) {
            e.printStackTrace();
            Log.i(TAG, "解码器错误 ：" + e.getMessage());
        }
    }


    @Override
    public void onReceveData(byte[] data) {
        Log.i(TAG, "onReceveData 接收数据 size = " + data.length);

        int index = mediaCodec.dequeueInputBuffer(100000);
        Log.i(TAG, "InputBuffer Index = " + index);
        if (index >= 0) {
            ByteBuffer inputBuffer = mediaCodec.getInputBuffer(index);
            inputBuffer.clear();
            inputBuffer.put(data, 0, data.length);
            mediaCodec.queueInputBuffer(index,
                    0, data.length, System.currentTimeMillis(), 0);
        }
        MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
        int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 100000);
        while (outputBufferIndex > 0) {
            mediaCodec.releaseOutputBuffer(outputBufferIndex, true
            );
            outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 100000);
            Log.i(TAG, "outputBuffer Index = " + outputBufferIndex);
        }
    }


}